import { IBizLabTemplate } from '@ibizlab/template';
import { kebabCase, upperFirst } from 'lodash';

/**
 * 导入插件助手 {{ImportPlugin 'form' ctrl}} {{ImportPlugin 'grid' ctrl}}
 *
 * @export
 * @class ImportPlugin
 */
export class ImportPlugin {
	/**
	 * Creates an instance of ImportPlugin.
	 * @param {IBizLabTemplate} template
	 * @memberof ImportPlugin
	 */
	constructor(template: IBizLabTemplate) {
		template.engine.registerHelper('importPlugin', (...args: unknown[]) => {
			const type = args[0];
			const ctrl = args[1] as any;
			switch (type) {
				case 'form':
					return this.computeFormPlugin(ctrl);
				case 'grid':
					return this.computeGridPlugin(ctrl);
				case 'panel':
					return this.computePanelPlugin(ctrl);
				case 'uiaction':
					return this.computeUIActionPlugin(ctrl);
				case 'demethod':
					return this.computeDEMethodPlugin(ctrl);
				case 'toolbar':
					return this.computeToolBarPlugin(ctrl);
				case 'tree':
					return this.computeTreePlugin(ctrl);
				default:
					return '';
			}
		});
	}

	/**
	 * 计算表单插件
	 *
	 * @param {*} ctrl 表单模型
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	public computeFormPlugin(ctrl: any): string {
		let result: string = '';
		//  表单成员插件数组
		const partPlugins: string[] = [];
		//  编辑器插件数组
		const editorPlugins: string[] = [];
		const formImport = (detail: any) => {
			//  表单成员插件
			if (detail.psSysPFPlugin && detail.psSysPFPlugin.pluginCode) {
				if (partPlugins.indexOf(detail.psSysPFPlugin.pluginCode) === -1) {
					partPlugins.push(detail.psSysPFPlugin.pluginCode);
				}
			}
			//  编辑器插件
			if (
				detail.psEditor &&
				detail.psEditor.psSysPFPlugin &&
				detail.psEditor.psSysPFPlugin.pluginCode
			) {
				if (
					editorPlugins.indexOf(detail.psEditor.psSysPFPlugin.pluginCode) === -1
				) {
					editorPlugins.push(detail.psEditor.psSysPFPlugin.pluginCode);
				}
			}
			if (detail.psDEFormDetails && detail.psDEFormDetails.length > 0) {
				detail.psDEFormDetails.forEach((item: any) => {
					formImport(item);
				});
			}
			if (detail.psDEFormTabPages && detail.psDEFormTabPages.length > 0) {
				detail.psDEFormTabPages.forEach((item: any) => {
					formImport(item);
				});
			}
		};
		//	表单无任何表单项成员则发布
		if (ctrl.psDEFormPages) {
			ctrl.psDEFormPages.forEach((formPage: any) => {
				if (formPage.psDEFormDetails && formPage.psDEFormDetails.length > 0) {
					formPage.psDEFormDetails.forEach((item: any) => {
						formImport(item);
					});
				}
			});
		}
		partPlugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(
				plugin
			)} from '@plugins/ctrl-item/${kebabCase(plugin)}/${kebabCase(
				plugin
			)}.vue';\n`;
		});
		editorPlugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(
				plugin
			)} from '@plugins/editor/${kebabCase(plugin)}/${kebabCase(
				plugin
			)}.vue';\n`;
		});
		return result;
	}

	/**
	 * 计算表格插件
	 *
	 * @param {*} ctrl
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	public computeGridPlugin(ctrl: any): string {
		let result: string = '';
		if (!ctrl || !ctrl.psDEGridColumns || ctrl.psDEGridColumns.length === 0) {
			return '';
		}
		//  表格列插件集合
		const columnPlugins: string[] = [];
		//  编辑器插件集合
		const editorPlugins: string[] = [];
		const editItems = ctrl.psDEGridEditItems || [];
		const pluginImport = (column: any) => {
			//  表格列插件
			if (column.psSysPFPlugin && column.psSysPFPlugin.pluginCode) {
				if (columnPlugins.indexOf(column.psSysPFPlugin.pluginCode) === -1) {
					columnPlugins.push(column.psSysPFPlugin.pluginCode);
				}
			}
			//  编辑器插件
			const editItem = editItems.find(
				(item: any) => item.codeName === column.codeName
			);
			if (
				editItem &&
				editItem.psEditor &&
				editItem.psEditor.psSysPFPlugin &&
				editItem.psEditor.psSysPFPlugin.pluginCode
			) {
				if (
					editorPlugins.indexOf(editItem.psEditor.psSysPFPlugin.pluginCode) ===
					-1
				) {
					editorPlugins.push(editItem.psEditor.psSysPFPlugin.pluginCode);
				}
			}
			if (column.psDEGridColumns && column.psDEGridColumns.length > 0) {
				column.psDEGridColumns.forEach((item: any) => {
					pluginImport(item);
				});
			}
		};
		ctrl.psDEGridColumns.forEach((column: any) => {
			pluginImport(column);
		});
		columnPlugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(
				plugin
			)} from '@plugins/ctrl-item/${kebabCase(plugin)}/${kebabCase(
				plugin
			)}.vue';`;
		});
		editorPlugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(
				plugin
			)} from '@plugins/editor/${kebabCase(plugin)}/${kebabCase(plugin)}.vue';`;
		});
		return result;
	}

	/**
	 * 插件代码映射对象
	 *
	 * @type {*}
	 * @memberof ImportPlugin
	 */
	public pluginCode: any = {
		'EDITOR_CUSTOMSTYLE': 'editor',
		'SEARCHBAR_ITEM': 'ctrl-item',
		'TOOLBAR_ITEM': 'ctrl-item',
		'PORTLET_TITLEBAR': 'ctrl-item',
		'PANEL_ITEM': 'ctrl-item',
		'MAPVIEW_ITEM': 'ctrl-item',
		'LIST_ITEMRENDER': 'ctrl-item',
		'GRID_COLRENDER': 'ctrl-item',
		'FORM_USERCONTROL': 'ctrl-item',
		'DATAVIEW_ITEM': 'ctrl-item',
		'DASHBOARD_ITEM': 'ctrl-item',
		'CHART_SERIESRENDER': 'ctrl-item',
		'CHART_CSRENDER': 'ctrl-item',
		'CHART_AXISRENDER': 'ctrl-item',
		'CALENDAR_ITEM': 'ctrl-item',
		'APPMENU_ITEM': 'ctrl-item',
		'AC_ITEM': 'ctrl-item',
		'APPMENU_RENDER': 'widget',
		'CALENDAR_RENDER': 'widget',
		'CHART_RENDER': 'widget',
		'CUSTOM': 'widget',
		'DASHBOARD_RENDER': 'widget',
		'EDITFORM_RENDER': 'widget',
		'GRID_RENDER': 'widget',
		'LIST_RENDER': 'widget',
		'MAPVIEW_RENDER': 'widget',
		'PANEL_RENDER': 'widget',
		'PORTLET_CUSTOM': 'widget',
		'SEARCHBAR_RENDER': 'widget',
		'SEARCHFORM_RENDER': 'widget',
		'TITLEBAR_RENDER': 'widget',
		'TOOLBAR_RENDER': 'widget',
		'TREE_RENDER': 'widget',
		'TREEEXPBAR_RENDER': 'widget',
		'WIZARDPANEL_RENDER': 'widget',
	}

	/**
	 * 计算面板插件
	 *
	 * @param {*} ctrl
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	public computePanelPlugin(ctrl: any): string {
		if (!ctrl || !ctrl.rootPSPanelItems || ctrl.rootPSPanelItems.length === 0) {
			return '';
		}
		let result: string = '';
		const plugins: any[] = [];
		const editorPlugins: any[] = [];
		const panelImport = (item: any) => {
			if (item.psSysPFPlugin) {
                const index = plugins.findIndex((plugin: any) => plugin.value == item.psSysPFPlugin.pluginCode);
                if (index === -1) {
                    plugins.push({ type: item.psSysPFPlugin.pluginType, value: item.psSysPFPlugin.pluginCode });
                }
			}
			if (item.itemType == 'FIELD' && item.psEditor && item.psEditor.psSysPFPlugin) {
                const index = editorPlugins.findIndex((plugin: any) => plugin.value == item.psEditor.psSysPFPlugin.pluginCode);
                if (index === -1) {
                    editorPlugins.push({ type: item.psEditor.psSysPFPlugin.pluginType, value: item.psEditor.psSysPFPlugin.pluginCode });
                }
			}
			if (item.psPanelTabPages && item.psPanelTabPages.length > 0) {
				item.psPanelTabPages.forEach((tabPage: any) => {
					panelImport(tabPage);
				})
			}
			if (item.psPanelItems && item.psPanelItems.length > 0) {
				item.psPanelItems.forEach((panelItem: any) => {
					panelImport(panelItem);
				})
			}
		}
		ctrl.rootPSPanelItems.forEach((item: any) => {
			panelImport(item);
		});
		plugins.forEach((plugin: any) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin.value)} from '@plugins/${this.pluginCode[plugin.type]}/${kebabCase(plugin.value)}/${kebabCase(plugin.value)}.vue';`
		});
		editorPlugins.forEach((plugin: any) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin.value)} from '@plugins/${this.pluginCode[plugin.type]}/${kebabCase(plugin.value)}/${kebabCase(plugin.value)}.vue';`
		});
		return result;
	}

	/**
	 * 导入界面行为插件
	 *
	 * @param {*} entity
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	public computeUIActionPlugin(entity: any): string {
		if (!entity || !entity.allPSAppDEUIActions || entity.allPSAppDEUIActions.length === 0) {
			return '';
		}
		let result: string = '';
		const plugins: string[] = [];
		entity.allPSAppDEUIActions.forEach((action: any) => {
			if (action && action.psSysPFPlugin && plugins.indexOf(action.psSysPFPlugin.pluginCode) === -1) {
				plugins.push(action.psSysPFPlugin.pluginCode);
			}
		});
		plugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin)} from '@/plugins/ui-action/${kebabCase(plugin)}/${kebabCase(plugin)}';`;
		});
		return result;
	}

	/**
	 * 导入工具栏项插件
	 *
	 * @param {*} entity
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	computeToolBarPlugin(ctrl: any) {
		if (!ctrl || !ctrl.psDEToolbarItems || ctrl.psDEToolbarItems.length === 0) {
			return '';
		}
		let result = '';
		const plugins: string[] = [];
		const toolBarImport = (item: any) => {
			if (
				item.psSysPFPlugin &&
				plugins.indexOf(item.psSysPFPlugin.pluginCode) === -1
			) {
				plugins.push(item.psSysPFPlugin.pluginCode);
			}
			if (item.psDEToolbarItems && item.psDEToolbarItems.length > 0) {
				item.psDEToolbarItems.forEach((toolbar_item: any) => {
					toolBarImport(toolbar_item);
				});
			}
		};
		ctrl.psDEToolbarItems.forEach((item: any) => {
			toolBarImport(item);
		});
		plugins.forEach((plugin) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin)} from '@plugins/ctrl-item/${kebabCase(plugin)}/${kebabCase(plugin)}.vue';`;
		});
		return result;
	}

	/**
	 * 导入树部件项插件
	 *
	 * @param {*} entity
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	computeTreePlugin(ctrl: any) {
		if (!ctrl || !ctrl.psDETreeNodes || ctrl.psDETreeNodes.length === 0) {
			return '';
		}
		let result = '';
		const plugins: string[] = [];
		const treeImport = (item: any) => {
			if (
				item.psSysPFPlugin &&
				plugins.indexOf(item.psSysPFPlugin.pluginCode) === -1
			) {
				plugins.push(item.psSysPFPlugin.pluginCode);
			}
		};
		ctrl.psDETreeNodes.forEach((item: any) => {
			treeImport(item);
		});
		plugins.forEach((plugin) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin)} from '@plugins/ctrl-item/${kebabCase(plugin)}/${kebabCase(plugin)}.vue';`;
		});
		return result;
	}

	/**
	 * 导入实体行为插件
	 *
	 * @param {*} entity
	 * @return {*}  {string}
	 * @memberof ImportPlugin
	 */
	public computeDEMethodPlugin(entity: any): string {
		if (!entity || !entity.allPSAppDEMethods || entity.allPSAppDEMethods.length === 0) {
			return '';
		}
		let result: string = '';
		const plugins: string[] = [];
		entity.allPSAppDEMethods.forEach((method: any) => {
			if (method && method.psSysPFPlugin && plugins.indexOf(method.psSysPFPlugin.pluginCode) === -1) {
				plugins.push(method.psSysPFPlugin.pluginCode);
			}
		});
		plugins.forEach((plugin: string) => {
			result += `${result === '' ? '' : '\n'}import ${upperFirst(plugin)} from '@/plugins/de-method/${kebabCase(plugin)}/${kebabCase(plugin)}';`;
		});
		return result;
	}
}
